2
תגובות
שתי שאלות בקשה .
1.איך פותרים את הבעיה עם ההודעת שגיעה?
2. איך אני מצליח לקרוא את drope list בסוף התוכנית.
תודה!
זו ההודעת שגיעה שאני מקבל
Multiple annotations found at this line:
- syntax error, unfinished class
declaration
- syntax error, unexpected '$_POST'
בשורות הבאות :
שסוגרות את ה if ובסוף התוכנית כאשר יש לי סוגרים מסולסלות.
קוד התוכנית :
1.איך פותרים את הבעיה עם ההודעת שגיעה?
2. איך אני מצליח לקרוא את drope list בסוף התוכנית.
תודה!
זו ההודעת שגיעה שאני מקבל
Multiple annotations found at this line:
- syntax error, unfinished class
declaration
- syntax error, unexpected '$_POST'
בשורות הבאות :
public $width = $_POST['width'];
public $height = $_POST['height'];
protected static $formula = 0;
public $height = $_POST['height'];
protected static $formula = 0;
שסוגרות את ה if ובסוף התוכנית כאשר יש לי סוגרים מסולסלות.
קוד התוכנית :
<h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" action="demo.php">
<br>width<input type=text name=num_a>
<br>height<input type=text name=num_b>
<br><input type="radio" name="shape" value="Rectangle" /> Rectangle
<br><input type="radio" name="shape" value="Tringle" /> Tringle
<br><input type=submit>
</form>
<?php
echo("<br>");
if(isset($_POST['submit']))
{echo "THERE is submit.!";
Class Rectangle {
//Declare the attributes:
public $width = $_POST['width'];
public $height = $_POST['height'];
protected static $formula = 0;
//Method to set the dimensions.
Function create_formula() {
self :: $formula = $this->width * $this->height;
}
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
$this->create_formula();
}
//Method to calculate and return the area.
function get_area() {
return (self::$formula);
}
}
Class Triangle extends Rectangle{
Function create_formula() {
self :: $formula = ($this->width * $this->height)/2;
}
}
if (!$_POST['shape']){echo('your choice is: '.$_POST['shape']);}
// create object of rectangle and calculate it is area
$rect = new Rectangle ();
echo ($rect->get_area()."<br />");
// create object of tringle and calculate it is area by extends
$tri = new Triangle ();
echo $tri->get_area();
}
?>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" action="demo.php">
<br>width<input type=text name=num_a>
<br>height<input type=text name=num_b>
<br><input type="radio" name="shape" value="Rectangle" /> Rectangle
<br><input type="radio" name="shape" value="Tringle" /> Tringle
<br><input type=submit>
</form>
<?php
echo("<br>");
if(isset($_POST['submit']))
{echo "THERE is submit.!";
Class Rectangle {
//Declare the attributes:
public $width = $_POST['width'];
public $height = $_POST['height'];
protected static $formula = 0;
//Method to set the dimensions.
Function create_formula() {
self :: $formula = $this->width * $this->height;
}
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
$this->create_formula();
}
//Method to calculate and return the area.
function get_area() {
return (self::$formula);
}
}
Class Triangle extends Rectangle{
Function create_formula() {
self :: $formula = ($this->width * $this->height)/2;
}
}
if (!$_POST['shape']){echo('your choice is: '.$_POST['shape']);}
// create object of rectangle and calculate it is area
$rect = new Rectangle ();
echo ($rect->get_area()."<br />");
// create object of tringle and calculate it is area by extends
$tri = new Triangle ();
echo $tri->get_area();
}
?>
2 תשובות
ענה
משתמש_193981
ב
03 למאי 2012
#
PHP מפענחת את הקוד שלך ויוצרת מחלקות עוד לפני שהקוד שלך מורץ.
עד עכשיו היית רגיל שהקוד שלך קורה שורה שורה. כשמדובר בפונקציות ומחלקות זה לא ככה. מנוע ה PHP קודם קורא את הקוד שלך כולו, מייצר את הפונקציות והמחלקות שלך אצלו בזיכרון ורק אז מתחיל לעבור שורה שורה ולהפעיל אותם.
כשאתה כותב
public $width = $_POST['width'];
מנוע ה PHP מבצע את השורה הזו הרבה לפני, שהוא בכלל מאתחל את המשתנה POST.
זה אומר שאתה לא יכול לגשת אל המשתנה POST בזמן הגדרת המחלקה.
במקום זה תעביר את אורך ורוחב בתור פרמטרים בעת יצירת מופע
<?php
Class Rectangle {
//Declare the attributes:
public $width ;
public $height ;
protected static $formula = 0;
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
//Method to set the dimensions.
Function create_formula()
{
self :: $formula = $this->width * $this->height;
}
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
$this->create_formula();
}
//Method to calculate and return the area.
function get_area() {
return (self::$formula);
}
}
Class Triangle extends Rectangle{
Function create_formula() {
self :: $formula = ($this->width * $this->height)/2;
}
}
?><h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" action="demo.php">
<br>width<input type=text name=num_a>
<br>height<input type=text name=num_b>
<br><input type="radio" name="shape" value="Rectangle" /> Rectangle
<br><input type="radio" name="shape" value="Tringle" /> Tringle
<br><input type=submit>
</form>
<?php
echo("<br>");
if(isset($_POST['submit']))
{echo "THERE is submit.!";
if (!$_POST['shape']){echo('your choice is: '.$_POST['shape']);}
// create object of rectangle and calculate it is area
$rect = new Rectangle ($_POST['width'], $_POST['height']);
echo ($rect->get_area()."<br />");
// create object of tringle and calculate it is area by extends
$tri = new Triangle ($_POST['width'], $_POST['height']);
echo $tri->get_area();
}
?>
Class Rectangle {
//Declare the attributes:
public $width ;
public $height ;
protected static $formula = 0;
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
//Method to set the dimensions.
Function create_formula()
{
self :: $formula = $this->width * $this->height;
}
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
$this->create_formula();
}
//Method to calculate and return the area.
function get_area() {
return (self::$formula);
}
}
Class Triangle extends Rectangle{
Function create_formula() {
self :: $formula = ($this->width * $this->height)/2;
}
}
?><h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" action="demo.php">
<br>width<input type=text name=num_a>
<br>height<input type=text name=num_b>
<br><input type="radio" name="shape" value="Rectangle" /> Rectangle
<br><input type="radio" name="shape" value="Tringle" /> Tringle
<br><input type=submit>
</form>
<?php
echo("<br>");
if(isset($_POST['submit']))
{echo "THERE is submit.!";
if (!$_POST['shape']){echo('your choice is: '.$_POST['shape']);}
// create object of rectangle and calculate it is area
$rect = new Rectangle ($_POST['width'], $_POST['height']);
echo ($rect->get_area()."<br />");
// create object of tringle and calculate it is area by extends
$tri = new Triangle ($_POST['width'], $_POST['height']);
echo $tri->get_area();
}
?>
---
ואגב, משולש לא יכול לירוש ממרובע :)
הם שניהם יכולים לירוש מ"צורה". אבל משולש הוא לא מרובע.
ככה שהקוד התקני יותר יהיה משהו כזה:
<?php
abstract class Shape
{
protected $width;
protected $height;
public function __construct($width, $height)
{
$this->SetSize($width, $height);
}
public function SetSize($width, $height)
{
$this->width = $width;
$this->height = $height;
}
public abstract function GetArea();
}
class Rectangle extends Shape
{
public function GetArea()
{
return $this->width * $this->height;
}
}
class Triangle extends Shape
{
public function GetArea()
{
return $this->width * $this->height * .5;
}
}
?>
<h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" >
<br>width<input type=text name='width'>
<br>height<input type=text name='height'>
<br><input type='submit' name='submit'>
</form>
<br/><br/>
<?php
if(isset($_POST['submit']))
{
// create object of rectangle and calculate it is area
$rect = new Rectangle ($_POST['width'], $_POST['height']);
echo 'rectangle of this size would have the area of ', $rect->GetArea(), "<br />";
// create object of tringle and calculate it is area by extends
$tri = new Triangle ($_POST['width'], $_POST['height']);
echo 'triangle of this sizes would have the area of ', $tri->GetArea();
}
?>
abstract class Shape
{
protected $width;
protected $height;
public function __construct($width, $height)
{
$this->SetSize($width, $height);
}
public function SetSize($width, $height)
{
$this->width = $width;
$this->height = $height;
}
public abstract function GetArea();
}
class Rectangle extends Shape
{
public function GetArea()
{
return $this->width * $this->height;
}
}
class Triangle extends Shape
{
public function GetArea()
{
return $this->width * $this->height * .5;
}
}
?>
<h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculate the area of rectangle or tringle and press submit...
<p>
<form method="post" >
<br>width<input type=text name='width'>
<br>height<input type=text name='height'>
<br><input type='submit' name='submit'>
</form>
<br/><br/>
<?php
if(isset($_POST['submit']))
{
// create object of rectangle and calculate it is area
$rect = new Rectangle ($_POST['width'], $_POST['height']);
echo 'rectangle of this size would have the area of ', $rect->GetArea(), "<br />";
// create object of tringle and calculate it is area by extends
$tri = new Triangle ($_POST['width'], $_POST['height']);
echo 'triangle of this sizes would have the area of ', $tri->GetArea();
}
?>
(אני מניח יש איזהשהו הסבר בהמשך המדריך שלך מה זה abstract)
ענה
משתמש_193357
ב
03 למאי 2012
#
WOW ,Thanks